import {
	getConfig
} from "../api/Interface/app";
import {
	swiperJumpType
} from "../enums/swiper";
import errorCode from "../utils/http/errorCode";
import {
	jumpPage
} from "../utils/route/route";
import { jump } from "../utils/route/tool";
import {
	useGetTokenStorage
} from "./useStorage";

/**
 * 点击轮播跳转
 * 轮播  type
 * 广告类型
 * 1.链接
 * 2.内部页面
 * 3.服务项(已去掉)
 * 4.无跳转
 */
export const useClickSwiperImg = (e, config = {}) => {
	const {
		type,
		applet_link
	} = e;
	switch (type) {
		case swiperJumpType.link:
			jumpPage({
				to: "/pages/webview/webview",
				query: {
					src: applet_link,
					type: "addtoken",
				},
				...config,
			});
			break;
		case swiperJumpType.path:
			if (!applet_link) return;
			jumpPage({
				to: applet_link,
				...config,
			});
			break;
		case swiperJumpType.service:
			if (!applet_link) return;
			jumpPage({
				to: applet_link,
				...config,
			});
			break;
		case swiperJumpType.none:
			break;
		default:
			break;
	}
};

/**
 * 获取配置
 * key
 * isHtml 协议类是否返回html页面0=否1=是
 * tipSpecial 组值tip是否格式化输出(房型时使用)
 */
export const useGetConfig = (params) => {
	if (!params.isHtml) {
		params.isHtml = 0;
	}
	return new Promise((resolve) => {
		getConfig(params).then((res) => {
			if (res.code === errorCode.success) {
				resolve(res.data.value);
			} else resolve([]);
		});
	});
};

/**
 * 点击金刚区跳转
 */
export const useClickArea = (e) => {
	// // but_type 按钮类型 1.链接 2.内部页面 3.酒店服务 4.日常服务
	// 金刚区  but_type 按钮类型 1.链接 2.内部页面 3.服务项(已去掉)
	const {
		but_content,
		but_name,
		but_type,
		icon,
		jingangButtonId
	} = e;
	switch (but_type) {
		// 链接
		case 1:
			jumpPage({
				to: "/pages/webview/webview",
				query: {
					src: but_content,
					type: "addtoken",
				},
			});
			break;
			// 内部页面
		case 2:
			if (!but_type) return;
			uni.navigateTo({
				url: but_content,
			});
			// const isLogin = !!useGetTokenStorage();
			// if (isLogin) {
			//   // uni.navigateTo({ url: "/package-hotel/pages/index" });
			//   uni.navigateTo({
			//     url: but_content,
			//   });
			// } else {
			//   uni.navigateTo({ url: "/package-hotel/pages/welcome" });
			// }
			break;
			// // 酒店服务
			// case 3:
			//   break;
			// // 日常服务
			// case 4:
			//   break;
		default:
			break;
	}
};

/** 获取当前页面路由(全路径,带挂餐) */
export const useGetCurrentRoute = () => {
  const pages = getCurrentPages();
  const curPage = pages[pages.length - 1];
  // return curPage.route;
  return curPage.$page.fullPath;
};

/** 获取当前页面路由(如:pages/index/index,不带参) */
export const useGetCurrentPageRoute = () => {
  const pages = getCurrentPages();
  const curPage = pages[pages.length - 1];
  return curPage.route;
};

/**
 * 返回上一页
 * delta: 1 返回页数
 * url: "/pages/index/index" delta失效 设置一个返回页
 * mode:"switchTab/redirectTo/navigateTo/reLaunch..." 跳转方式
 * query:null 携带参数对象
 */
export const usePageBack = ({
  delta = 1,
  url = "/pages/index/index",
  mode = "switchTab",
  query,
}) => {
  const to = url || "/pages/index/index";
  console.log("usePageBack", delta, url, mode, query);
  try {
    uni.navigateBack({
      delta,
      fail(e) {
        console.log("usePageBack fail", e, delta, url, mode, query);
        jump({
          to,
          mode,
          query,
        });
      },
    });
  } catch (e) {
    console.log("usePageBack catch", e, delta, url, mode, query);
    uni.switchTab({
      url: "/pages/index/index",
    });
  }
};